package com.restfinder.news.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.restfinder.comment.model.Comment;
import com.restfinder.model.IEntity;
import com.restfinder.user.model.Role;

import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name="news")
public class News implements IEntity {

	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue
	@Getter @Setter
	protected long id;
	@Getter @Setter
	protected Date date;
	@Getter @Setter
	protected String title;
	@Getter @Setter
	protected String content;
	@OneToMany(cascade=CascadeType.ALL, mappedBy="news", targetEntity=Comment.class)
	@Getter @Setter
	protected List<Comment> comments;
	@Column(name="news_type_value", insertable=false, updatable=false)
	@Getter @Setter
	protected String newsTypeValue;
	
	public void add(Comment comment){
		if(comments==null){
			comments = new ArrayList<Comment>();
		}
		this.comments.add(comment);
	}
	
	@Override	//TODO z dziedziczeniami
	public boolean equals(Object obj) {
		if(this == obj) return true;
		if ((obj == null) || !(obj instanceof News)) {
			return false;
		}
		News objRole = (News) obj;
		return objRole.getId() == this.id;		
	}

	@Override
	public String toString() {
		return "News.id="+this.id;
	}
	
	

}
